use crate::math::Vector2u;

use super::rhi::Rhi;

pub struct RenderTarget;

impl RenderTarget {
    pub fn create_render_attachment_texture(
        rhi: Rhi,
        screen_rect: Vector2u,
    ) -> (wgpu::Texture,wgpu::TextureView, wgpu::BindGroup) {
        let size = wgpu::Extent3d {
            width: screen_rect.x,
            height: screen_rect.y,
            depth_or_array_layers: 1,
        };
        let texture = rhi
            .borrow()
            .device
            .create_texture(&wgpu::TextureDescriptor {
                label: Some("render attachment"),
                size,
                mip_level_count: 1,
                sample_count: 1,
                dimension: wgpu::TextureDimension::D2,
                format: wgpu::TextureFormat::Bgra8UnormSrgb,
                usage: wgpu::TextureUsages::RENDER_ATTACHMENT
                    | wgpu::TextureUsages::TEXTURE_BINDING,
            });

        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
        let sampler = rhi
            .borrow()
            .device
            .create_sampler(&wgpu::SamplerDescriptor {
                address_mode_u: wgpu::AddressMode::ClampToEdge,
                address_mode_v: wgpu::AddressMode::ClampToEdge,
                address_mode_w: wgpu::AddressMode::ClampToEdge,
                mag_filter: wgpu::FilterMode::Linear,
                min_filter: wgpu::FilterMode::Nearest,
                mipmap_filter: wgpu::FilterMode::Nearest,
                ..Default::default()
            });

        Self::bind_texture_group(rhi, texture, view, sampler)
    }


    pub fn get_render_attachment_layout(rhi:Rhi) -> wgpu::BindGroupLayout {
        rhi.borrow()
            .device
            .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                entries: &[
                    wgpu::BindGroupLayoutEntry {
                        binding: 0,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Texture {
                            multisampled: false,
                            view_dimension: wgpu::TextureViewDimension::D2,
                            sample_type: wgpu::TextureSampleType::Float { filterable: true },
                        },
                        count: None,
                    },
                    wgpu::BindGroupLayoutEntry {
                        binding: 1,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                        count: None,
                    },
                    ],
                label: Some("bind base color texture layout"),
            })
    }

    pub fn bind_texture_group(
        rhi: Rhi,
        texture: wgpu::Texture,
        view: wgpu::TextureView,
        sampler: wgpu::Sampler,
    ) -> (wgpu::Texture,wgpu::TextureView, wgpu::BindGroup) {
        let render_attachment_layout = Self::get_render_attachment_layout(rhi.clone());
        let pbr_material_group =
            rhi.borrow()
                .device
                .create_bind_group(&wgpu::BindGroupDescriptor {
                    layout: &render_attachment_layout,
                    entries: &[
                        wgpu::BindGroupEntry {
                            binding: 0,
                            resource: wgpu::BindingResource::TextureView(&view),
                        },
                        wgpu::BindGroupEntry {
                            binding: 1,
                            resource: wgpu::BindingResource::Sampler(&sampler),
                        },
                    ],
                    label: Some("diffuse_bind_group"),
                });

        (texture, view , pbr_material_group)
    }
}
